#include "params.h"

Parameters::Parameters(Message* msg) {

	// initialise data
	message = msg;
	stop = new StopCondition(message);

	verbose = 0;
	color   = 0;
	index	= 4;
	
	do_compress = 0;
	do_decompress = 0;
	
	has_error = 0;
	
	// the default condition is exit on 60 seconds
	// we don't want to leave StopCondition without any
	// condition, since it will run forever
	stop->addStopOnSeconds(60);
}

void Parameters::analyseComandLine(int argc, char** argv) {
	
	// analyse arguments

	if (argc<4) {
		// error: not enough arguments; we'll simply show the usage, no error 
		// will be reported
		has_error=1;
		return;
	}
		
	// process the command argument
	if(strcmp(argv[1],"c")==0)
		do_compress = 1;
	else if(strcmp(argv[1],"x")==0)
		do_decompress = 1;
	else {
		has_error = 1;
		message->printError("ERROR: Incorrect action ");
		message->printError(argv[1]);
		message->printError(".\n");
	}
	
	if (strcmp(argv[2],"")==0){
		has_error = 1;
		message->printError("ERROR: Incorrect input filename.\n");
	}
	
	if (strcmp(argv[3],"")==0){
		has_error = 1;
		message->printError("ERROR: Incorrect output filename.\n");
	}

	infile=argv[2];
	outfile=argv[3];
	
	// process the options
	while(index<argc)
	{
		if(strcmp(argv[index],"--color")==0)	{
			color=1;
			index++;
			continue;
		}

		if(strcmp(argv[index],"--verbose")==0) {
			verbose=1;
			index++;
			continue;
		}

		if((strcmp(argv[index],"--sec")==0)&&(index+1<argc)) {
			stop->addStopOnSeconds(atoi(argv[index+1]));
			index+=2;
			continue;
		}

		if((strcmp(argv[index],"--snr")==0)&&(index+1<argc)) {
			stop->addStopOnSNR(atof(argv[index+1]));
			index+=2;
			continue;
		}

		if((strcmp(argv[index],"--iter")==0)&&(index+1<argc)) {
			stop->addStopOnIterations(atoi(argv[index+1]));
			index+=2;
			continue;
		}
		
		message->printError("WARNING: Incorrect option ");
		message->printError(argv[index]);
		message->printError(". Ignored.\n");
		
		index++;
	}
}



int Parameters::mustDecompress(){
	return do_decompress;
}



int Parameters::mustCompress(){
	return do_compress;	
}



const char* Parameters::getInputFilename(){
	return infile;
}



const char* Parameters::getOutputFilename(){
	return outfile;
}



int Parameters::getVerbose(){
	return verbose;
}



int Parameters::getColor(){
	return color;
}



void Parameters::setColor(int value){
	color=value;
}


StopCondition* Parameters::getStopCondition() {
	return stop;
}


int Parameters::hasErrors() {
	return has_error;
}




